import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/bean/bean_tools_category.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/bean/event_bus_select_tool_category.dart';
import 'package:event_bus/event_bus.dart';
import 'package:dk_flutter_base/dk_core.dart';


typedef onPressed = void Function(int index);

//todo:点击按钮，更改显示状态，切换页面数据
class ToolsCategoryButton extends StatelessWidget {
  final String text;
  final int index;
  final bool isSlected;
  final onPressed pressed;

  ToolsCategoryButton(
    this.text,
    this.isSlected,
    this.index,
    this.pressed,
  );

  Widget build(BuildContext context) {
    Color textColor = isSlected ? Colors.white : Color(0xffA6C3FC);
    Color borderColor = isSlected ? Colors.white : Color(0xff4E87F9);
    Color color = Color(0xff4E87F9);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
          alignment: Alignment.center,
          child:
              DkText.simple(text, 15, textColor, fontWeight: FontWeight.normal),
          decoration: BoxDecoration(
            border: Border.all(color: borderColor, width: 0.5),
            color: color,
            borderRadius: BorderRadius.horizontal(
                left: Radius.circular(13), right: Radius.circular(13)),
          )),
      onTap: () {
        print('点击$text');
        pressed(index);
      },
    );
  }
}

//工具分类栏目条
class ToolsCategoryBarWidget extends StatefulWidget {
  final List<ToolsCategoryBean> categoryList;
  final EventBus eventBus;
  final int selectedIndex;
  ToolsCategoryBarWidget(this.categoryList,this.selectedIndex, this.eventBus);
  @override
  _ToolsCategoryBarWidgetState createState() => _ToolsCategoryBarWidgetState();
}

class _ToolsCategoryBarWidgetState extends State<ToolsCategoryBarWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      alignment: Alignment.center,
       decoration: BoxDecoration(
               color: Color(0xff3274F9),
               border: Border(bottom: BorderSide(width: 0.0,color: Color(0xff3274F9))),
            ),
      padding: EdgeInsets.symmetric(vertical: 11),
      child: ListView.builder(
        padding: EdgeInsets.symmetric(horizontal: 16),
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        itemCount: widget.categoryList.length,
        itemBuilder: (BuildContext context, int index) {
          ToolsCategoryBean categoryBean = widget.categoryList[index];
          bool isSelected = false;
          if (index == widget.selectedIndex) {
            isSelected = true;
          }
          return Padding(
            padding: EdgeInsets.only(right: 9),
            child: ToolsCategoryButton(
              categoryBean.categoryName,
              isSelected,
              index,
              (int index) {
                widget.eventBus?.fire(SelectToolCategoryResultEvent(index));
                print('点击$index 分类');
              },
            ),
          );
        },
      ),
    );
  }
}
